﻿#include "order_handle.h"
#include <QDebug>

order_handle::order_handle(QObject *parent) :
    QObject(parent)
{
}

QByteArray order_handle::make_codeDownload_order(char addr,
                                                 FUNCODE func,
                                                 QString data)
{
    QByteArray order;
    int i;
    int dataSize = data.size();
    order.resize(PRESIZE + STARTSIZE + ADDRSIZE +
                 FUNCODESIZE + LENSIZE + dataSize +
                 1 + CHECKSIZE + ENDSIZE);

    for(i = 0; i < PRESIZE; i++){
        order[i] = 0xFE;
    }
    order[i++] = 0x68;//起始
    order[i++] = addr;//地址
    order[i++] = 0xF0;//功能码
    order[i++] = data.size()+1;//长度
    order[i++] = func;//副功能码
    order.replace(i++, data.size(), data.toLatin1());

    i += data.size()-1;
    order[i++] = 0xAB;//校验
    order[i++] = 0x16;//结束
    return order;
}

QByteArray order_handle::make_codeDownload_order(char addr,
                                                 FUNCODE func,
                                                 QByteArray data)
{
    QByteArray order;
    int i;
    int dataSize = data.size();
    order.resize(PRESIZE + STARTSIZE + ADDRSIZE +
                 MAINCSIZE + LENSIZE + dataSize +
                 FUNCODESIZE + CHECKSIZE + ENDSIZE);

    for(i = 0; i < PRESIZE; i++){
        order[i] = 0xFE;
    }
    order[i++] = 0x68;//起始
    order[i++] = addr;//地址
    order[i++] = 0xF0;//功能码
    order[i++] = data.size()+1;//长度
    order[i++] = func;//副功能码

    order.replace(i++, data.size(), data);

    i += data.size()-1;
    order[i++] = checkdata(func, data);//校验
    order[i++] = 0x16;//结束
    return order;
}

//QByteArray order_handle::make_codeRun_order(char addr,
//                                                 FUNCODE func,
//                                                 QByteArray data)
//{
//    QByteArray order;
//    int i;
//    int dataSize = data.size();
//    order.resize(PRESIZE + STARTSIZE + ADDRSIZE +
//                 MAINCSIZE + LENSIZE + dataSize +
//                 FUNCODESIZE + CHECKSIZE + ENDSIZE);

//    for(i = 0; i < PRESIZE; i++){
//        order[i] = 0xFE;
//    }
//    order[i++] = 0x68;//起始
//    order[i++] = addr;//地址
//    order[i++] = 0xF0;//功能码
//    order[i++] = data.size()+1;//长度
//    order[i++] = func;//副功能码

//    order.replace(i++, data.size(), data);

//    i += data.size()-1;
//    order[i++] = checkdata(func, data);//校验
//    order[i++] = 0x16;//结束
//    return order;
//}

//计算校验
char order_handle::checkdata(char func, QByteArray data)
{
    unsigned char count = 0;
    int i;
    for(i = 0; i < data.size(); i++){
        count += data[i];
    }
    count += func;
    count &= 0xFF;
    return count;
}
//分包预处理
QList<QByteArray> order_handle::data_subpackage(QString data, int pakLen)
{
    QList<QByteArray> subpackage;
    QByteArray tmp = data.toLocal8Bit();
    if(pakLen == 0){//新版本长度拓展
        pakLen = 1024;
    }

    while(tmp.size() > 0){
        subpackage<<tmp.left(pakLen-1);
        tmp.remove(0,pakLen-1);
//        qDebug()<<tmp<<tmp.size();
    }
//    qDebug()<<"subpackage"<<subpackage.at(0)
//           <<subpackage.at(1).toHex()
//           <<subpackage.size();
    return subpackage;
}

//生成发送完毕包
QByteArray order_handle::make_endpackage(char addr)
{
    QByteArray order;
    int i;
    order.resize(PRESIZE + STARTSIZE + ADDRSIZE +
                 MAINCSIZE + LENSIZE +
                 FUNCODESIZE + CHECKSIZE + ENDSIZE);

    for(i = 0; i < PRESIZE; i++){
        order[i] = 0xFE;
    }
    order[i++] = 0x68;//起始
    order[i++] = addr;//地址
    order[i++] = 0xF0;//功能码
    order[i++] = 1;//长度
    order[i++] = 0x02;//副功能码
    order[i++] = 0x02;//校验
    order[i++] = 0x16;//结束
    return order;
}

//检查返回数据
int order_handle::check_returnOrder(QByteArray data)
{
    int get = 0;
    for(int i = 0; i < data.length(); i++){
        if(data[i] == (char)0x68){
//            qDebug()<<"find 0x68"<<data[i]<<i;
            get = i+2;
            break;
        }
    }

    if(data[get] == (char)0xf0){
        return 0;//正常
    }else if(data[get] == (char)0xfe){
        return -1;//重发
    }else if(data[get] == (char)0xff){
        return -2;//传送终止
    }else{
        return -3;
    }
}
